//
//  TestCollectionVPageVC.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2022/9/7.
//  Copyright © 2022 CocoaPods. All rights reserved.
//

import UIKit
import CooperSwift
import RxSwift
import RxDataSources

class TestCollectionVPageVM: NSObject, CollectionViewModelProtocol, RefreshControllable, UICollectionViewDelegate {
    
    func configDown() {
        print("TestPageContent3VM configDown")
        addFirstSectionItems([
            TestCollectionCellModel.initWith("测试1", size: CGSize(80, 120)).addTargetKey(bindKey()),
            TestCollectionCellModel.initWith("测试2").addTargetKey(bindKey()),
            TestCollectionCellModel.initWith("测试1").addTargetKey(bindKey()),
            TestCollectionCellModel.initWith("测试2").addTargetKey(bindKey()),
            TestCollectionCellModel.initWith("测试1", size: CGSize(80, 120)).addTargetKey(bindKey()),
            TestCollectionCellModel.initWith("测试2").addTargetKey(bindKey()),
            TestCollectionCellModel.initWith("测试1").addTargetKey(bindKey()),
            TestCollectionCellModel.initWith("测试2").addTargetKey(bindKey()),
        ])
        
        toState(.loading)
        debugPrint("loading")
        PublicMethod.delay(3) { [weak self] in
            debugPrint("normal")
            self?.toState(.success)
        }
    }
    
    @objc func sumbit()  {
        log.debug("sumbit")
    }
    
    func refresh() {
        log.debug("TestPageContent3VM refresh")
        PublicMethod.delay(2) {
            self.rx.refreshStatus.onNext(.endHeaderRefresh)
        }
    }
    
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//        return vm_collectionView(collectionView, layout: collectionViewLayout, sizeForItemAt: indexPath)
//    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
    }
    
}


class TestCollectionVPageVC: UIViewController, Refreshable {

    @IBOutlet weak var cv: UICollectionView!
    
    let vm = ViewModelFactory.collectionVM(TestCollectionVPageVM.self)
    
    let bag = DisposeBag()

    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        cv.collectionViewLayout = UICollectionViewFlowLayout.init().defaultLayout()
    
        /// 绑定view model
        ViewBindVMHelper.bindCVVM(cv, vm, bag: bag)
        rx.updateRefresh(vm, scrollview: cv, bag: bag, refreshConfig: .all)
        

//        /// rx 来自 public extension Refreshable where Self: NSObject & TableEnableProtocol
//        rx.tapEmptyButton(cv) { _ in
//            vm.refresh()
//        }
        
        
//        configAll(vm, disposeBag: disposeBag, flowLayout:  UICollectionViewFlowLayout.init().defaultLayout())
        
//        cv.load(cell: <#T##AnyClass#>, flowLayout: <#T##UICollectionViewFlowLayout#>)
        
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
